package com.minimalist.common.entity.basic.po;

import com.mybatisflex.annotation.Column;
import com.mybatisflex.annotation.Table;
import com.minimalist.common.config.mybatis.InsertFullColumnHandler;
import com.minimalist.common.config.mybatis.UpdateFullColumnHandler;
import com.minimalist.common.config.mybatis.bo.BaseEntity;

import java.io.Serial;
import java.io.Serializable;
import java.util.Objects;

/**
 * 用户表实体类
 * <p>
 * 用于管理系统用户的基本信息，包括用户账号、密码、个人信息、
 * 状态管理等功能，支持多租户架构
 *
 * @author 小太阳
 * @since 2024-10-18
 */
@Table(value = "m_user", onInsert = InsertFullColumnHandler.class, onUpdate = UpdateFullColumnHandler.class)
public class MUser extends BaseEntity implements Serializable {
    
    @Serial
    private static final long serialVersionUID = 1L;

    // ========== 主要业务字段 ==========
    
    /**
     * 用户ID
     */
    private Long userId;

    /**
     * 用户账号
     */
    private String username;

    /**
     * 密码
     */
    private String password;

    /**
     * 盐值
     */
    private String salt;

    /**
     * 用户昵称
     */
    private String nickname;

    /**
     * 用户真实姓名
     */
    private String userRealName;

    /**
     * 用户邮箱
     */
    private String email;

    /**
     * 手机号码
     */
    private String phone;

    /**
     * 用户性别 0未知 1男 2女
     */
    private Integer userSex;

    /**
     * 头像base64编码
     */
    private String userAvatar;

    /**
     * 备注
     */
    private String remark;

    /**
     * 状态 0禁用 1正常
     */
    private Integer status;

    /**
     * 租户编号
     */
    @Column(tenantId = true)
    private Long tenantId;

    // ========== 构造函数 ==========
    
    /**
     * 默认构造函数
     */
    public MUser() {
    }

    /**
     * 全参数构造函数
     *
     * @param userId       用户ID
     * @param username     用户账号
     * @param password     密码
     * @param salt         盐值
     * @param nickname     用户昵称
     * @param userRealName 用户真实姓名
     * @param email        用户邮箱
     * @param phone        手机号码
     * @param userSex      用户性别
     * @param userAvatar   头像base64编码
     * @param remark       备注
     * @param status       状态
     * @param tenantId     租户编号
     */
    public MUser(Long userId, String username, String password, String salt, String nickname, 
                String userRealName, String email, String phone, Integer userSex, String userAvatar, 
                String remark, Integer status, Long tenantId) {
        this.userId = userId;
        this.username = username;
        this.password = password;
        this.salt = salt;
        this.nickname = nickname;
        this.userRealName = userRealName;
        this.email = email;
        this.phone = phone;
        this.userSex = userSex;
        this.userAvatar = userAvatar;
        this.remark = remark;
        this.status = status;
        this.tenantId = tenantId;
    }

    /**
     * 基本信息构造函数
     *
     * @param username     用户账号
     * @param password     密码
     * @param nickname     用户昵称
     * @param userRealName 用户真实姓名
     * @param email        用户邮箱
     */
    public MUser(String username, String password, String nickname, String userRealName, String email) {
        this.username = username;
        this.password = password;
        this.nickname = nickname;
        this.userRealName = userRealName;
        this.email = email;
        this.status = 1; // 默认启用状态
    }

    // ========== Getter 和 Setter 方法 ==========
    
    public Long getUserId() {
        return userId;
    }

    public void setUserId(Long userId) {
        this.userId = userId;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getSalt() {
        return salt;
    }

    public void setSalt(String salt) {
        this.salt = salt;
    }

    public String getNickname() {
        return nickname;
    }

    public void setNickname(String nickname) {
        this.nickname = nickname;
    }

    public String getUserRealName() {
        return userRealName;
    }

    public void setUserRealName(String userRealName) {
        this.userRealName = userRealName;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public Integer getUserSex() {
        return userSex;
    }

    public void setUserSex(Integer userSex) {
        this.userSex = userSex;
    }

    public String getUserAvatar() {
        return userAvatar;
    }

    public void setUserAvatar(String userAvatar) {
        this.userAvatar = userAvatar;
    }

    public String getRemark() {
        return remark;
    }

    public void setRemark(String remark) {
        this.remark = remark;
    }

    public Integer getStatus() {
        return status;
    }

    public void setStatus(Integer status) {
        this.status = status;
    }

    public Long getTenantId() {
        return tenantId;
    }

    public void setTenantId(Long tenantId) {
        this.tenantId = tenantId;
    }

    // ========== Object 方法重写 ==========
    
    @Override
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (o == null || getClass() != o.getClass()) {
            return false;
        }
        if (!super.equals(o)) {
            return false;
        }
        
        MUser mUser = (MUser) o;
        
        return Objects.equals(userId, mUser.userId) &&
               Objects.equals(username, mUser.username) &&
               Objects.equals(password, mUser.password) &&
               Objects.equals(salt, mUser.salt) &&
               Objects.equals(nickname, mUser.nickname) &&
               Objects.equals(userRealName, mUser.userRealName) &&
               Objects.equals(email, mUser.email) &&
               Objects.equals(phone, mUser.phone) &&
               Objects.equals(userSex, mUser.userSex) &&
               Objects.equals(userAvatar, mUser.userAvatar) &&
               Objects.equals(remark, mUser.remark) &&
               Objects.equals(status, mUser.status) &&
               Objects.equals(tenantId, mUser.tenantId);
    }

    @Override
    public int hashCode() {
        return Objects.hash(super.hashCode(), userId, username, password, salt, nickname, 
                           userRealName, email, phone, userSex, userAvatar, remark, status, tenantId);
    }

    @Override
    public String toString() {
        return "MUser{" +
               "userId=" + userId +
               ", username='" + username + '\'' +
               ", nickname='" + nickname + '\'' +
               ", userRealName='" + userRealName + '\'' +
               ", email='" + email + '\'' +
               ", phone='" + phone + '\'' +
               ", userSex=" + userSex +
               ", remark='" + remark + '\'' +
               ", status=" + status +
               ", tenantId=" + tenantId +
               "} " + super.toString();
    }

    // ========== Builder 模式 ==========
    
    /**
     * 创建Builder实例
     *
     * @return Builder实例
     */
    public static Builder builder() {
        return new Builder();
    }

    /**
     * Builder类，用于构建MUser实例
     */
    public static class Builder {
        private Long userId;
        private String username;
        private String password;
        private String salt;
        private String nickname;
        private String userRealName;
        private String email;
        private String phone;
        private Integer userSex;
        private String userAvatar;
        private String remark;
        private Integer status;
        private Long tenantId;

        Builder() {
        }

        public Builder userId(Long userId) {
            this.userId = userId;
            return this;
        }

        public Builder username(String username) {
            this.username = username;
            return this;
        }

        public Builder password(String password) {
            this.password = password;
            return this;
        }

        public Builder salt(String salt) {
            this.salt = salt;
            return this;
        }

        public Builder nickname(String nickname) {
            this.nickname = nickname;
            return this;
        }

        public Builder userRealName(String userRealName) {
            this.userRealName = userRealName;
            return this;
        }

        public Builder email(String email) {
            this.email = email;
            return this;
        }

        public Builder phone(String phone) {
            this.phone = phone;
            return this;
        }

        public Builder userSex(Integer userSex) {
            this.userSex = userSex;
            return this;
        }

        public Builder userAvatar(String userAvatar) {
            this.userAvatar = userAvatar;
            return this;
        }

        public Builder remark(String remark) {
            this.remark = remark;
            return this;
        }

        public Builder status(Integer status) {
            this.status = status;
            return this;
        }

        public Builder tenantId(Long tenantId) {
            this.tenantId = tenantId;
            return this;
        }

        public MUser build() {
            return new MUser(userId, username, password, salt, nickname, userRealName, 
                           email, phone, userSex, userAvatar, remark, status, tenantId);
        }

        @Override
        public String toString() {
            return "MUser.Builder{" +
                   "userId=" + userId +
                   ", username='" + username + '\'' +
                   ", nickname='" + nickname + '\'' +
                   ", userRealName='" + userRealName + '\'' +
                   ", email='" + email + '\'' +
                   ", phone='" + phone + '\'' +
                   ", userSex=" + userSex +
                   ", remark='" + remark + '\'' +
                   ", status=" + status +
                   ", tenantId=" + tenantId +
                   '}';
        }
    }

    // ========== 便利方法 ==========
    
    /**
     * 检查用户是否启用
     *
     * @return true如果用户状态为启用，否则false
     */
    public boolean isEnabled() {
        return Integer.valueOf(1).equals(this.status);
    }

    /**
     * 检查用户是否禁用
     *
     * @return true如果用户状态为禁用，否则false
     */
    public boolean isDisabled() {
        return Integer.valueOf(0).equals(this.status);
    }

    /**
     * 启用用户
     */
    public void enable() {
        this.status = 1;
    }

    /**
     * 禁用用户
     */
    public void disable() {
        this.status = 0;
    }

    /**
     * 检查是否有邮箱
     *
     * @return true如果邮箱不为空，否则false
     */
    public boolean hasEmail() {
        return email != null && !email.trim().isEmpty();
    }

    /**
     * 检查是否有手机号
     *
     * @return true如果手机号不为空，否则false
     */
    public boolean hasPhone() {
        return phone != null && !phone.trim().isEmpty();
    }
}
